package tyk.Servlet;

import tyk.Service.AccountManagementService;
import tyk.pojo.AccountPage;
import tyk.pojo.Merchant;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/***
 *@title $NAME
 *@description <TODO description class purpose>
 *@author Tang'ya'kang
 *@VERSION 1.0.0
 *@CREATE 2023/5/23 1:00
 **/
public class AccountManagementServlet extends HttpServlet {
    public void SelectAccountManagementByPage(HttpServletRequest request,HttpServletResponse response) throws SQLException, InstantiationException, IllegalAccessException, NoSuchFieldException, ServletException, IOException {
        Integer page= Integer.valueOf(request.getParameter("Page"));
        AccountManagementService accountManagementService=new AccountManagementService();
        AccountPage accountPage=new AccountPage();
        accountPage.setPage(page);
        List<Merchant> list=new ArrayList<>();
        list=accountManagementService.SelectAccountManagementByPage(page,accountPage);
        request.setAttribute("Merchants",list);
        request.setAttribute("AccountPage",accountPage);
        request.setAttribute("url","./accountManagementServlet?action=SelectAccountManagementByPage&Page=");
        request.getRequestDispatcher("./manage/AccountManagement.jsp").forward(request,response);
    }
    public void DeleteAccountManagementById(HttpServletRequest request,HttpServletResponse response) throws SQLException, ServletException, IOException {
        Integer Id= Integer.valueOf(request.getParameter("Id"));
        Integer page= Integer.valueOf(request.getParameter("Page"));
        AccountManagementService accountManagementService=new AccountManagementService();
        accountManagementService.DeleteAccountManagementById(Id);
        request.setAttribute("url","./accountManagementServlet?action=SelectAccountManagementByPage&Page=");
        response.sendRedirect("./accountManagementServlet?action=SelectAccountManagementByPage&Page="+page);
    }
    public void EditAccountManagementById(HttpServletRequest request,HttpServletResponse response) throws SQLException, InstantiationException, IllegalAccessException, NoSuchFieldException, ServletException, IOException {
        Integer Id= Integer.valueOf(request.getParameter("Id"));
        Integer page= Integer.valueOf(request.getParameter("Page"));
        AccountManagementService accountManagementService=new AccountManagementService();
        Merchant merchant=accountManagementService.EditAccountManagementById(Id,page);
        request.setAttribute("merchant",merchant);
        request.setAttribute("Page",page);
        request.setAttribute("url","./accountManagementServlet?action=SelectAccountManagementByPage&Page=");
        request.getRequestDispatcher("./manage/EditAccount.jsp").forward(request,response);
    }
    public void EditAccount(HttpServletRequest request,HttpServletResponse response) throws SQLException, ServletException, IOException {
        Integer page= Integer.valueOf(request.getParameter("Page"));
        Integer cnt= Integer.valueOf(request.getParameter("cnt"));
        Integer user_id= Integer.valueOf(request.getParameter("user_id"));
        AccountManagementService accountManagementService=new AccountManagementService();
        accountManagementService.EditAccount(user_id,cnt);
        request.setAttribute("url","./accountManagementServlet?action=SelectAccountManagementByPage&Page=");
        response.sendRedirect("./accountManagementServlet?action=SelectAccountManagementByPage&Page="+page);
    }
    public void AddAccount(HttpServletRequest request,HttpServletResponse response) throws SQLException, InstantiationException, IllegalAccessException, NoSuchFieldException, IOException {
        System.out.println("到此一游");
        Integer page= Integer.valueOf(request.getParameter("Page"));
        Integer cnt= Integer.valueOf(request.getParameter("cnt"));
        AccountManagementService accountManagementService=new AccountManagementService();
        String user_name=request.getParameter("user_name");
        Integer user_id=accountManagementService.SelectAccountByUser_name(user_name);
        System.out.println("user_id->"+user_id);
        Merchant merchant=new Merchant(user_id,user_name,cnt);
        System.out.println(merchant);
        accountManagementService.AddAccount(merchant);
        request.setAttribute("url","./accountManagementServlet?action=SelectAccountManagementByPage&Page=");
        response.sendRedirect("./accountManagementServlet?action=SelectAccountManagementByPage&Page="+page);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String hidden=request.getParameter("action");
        try {
            Method method = this.getClass().getDeclaredMethod(hidden,HttpServletRequest.class,HttpServletResponse.class);
            method.invoke(this,request,response);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
